package com.woniu.job.controller;

/**
 * @author : 陈思宇
 * @date :2019/12/26
 */

import com.woniu.job.entity.Resume;
import com.woniu.job.entity.vo.ResultVO;

import com.woniu.job.mapper.CompanyMapper;
import com.woniu.job.mapper.ResumeMapper;
import com.woniu.job.util.WordUtil;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping({"/files"})
class UploadAndDownloadController {

    @Value("${files.location}")
    String path;

    @Autowired
    private ResumeMapper resumeMapper;

    @Autowired
    private CompanyMapper companyMapper;

    public final static String UPLOAD_PATH_PREFIX = "static/uploadFile/";

    /**
     * 上传企业营业执照
     *
     * @param files
     * @return
     */
    @RequestMapping({"/permit"})
    public ResultVO uploadPermit(@RequestParam("file") MultipartFile[] files) {
        ResultVO responseVO = new ResultVO();
        MultipartFile[] var4 = files;
        int var5 = files.length;

        for (int var6 = 0; var6 < var5; ++var6) {
            MultipartFile file = var4[var6];
            if (file.isEmpty()) {
                responseVO.setCode(500);
                responseVO.setMessage("请上传正确的文件格式");
            } else {
                //得到文件名
                String filName = file.getOriginalFilename();
                //取出文件后缀
                String suffix = filName.substring(filName.lastIndexOf("."));
                //存入文件地址
                String perms = UUID.randomUUID() + suffix;
                String paths = path + perms;
                File newFile = new File(paths);
                // 如果pathAll路径不存在，则创建相关该路径涉及的文件夹;
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
                try {
                    //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
                    file.transferTo(newFile);
                    responseVO.setResult(true);
                    responseVO.setCode(200);
                    responseVO.setMessage("成功");
                    responseVO.setData("/files/" + perms);
                } catch (IOException var13) {
                    responseVO.setCode(-500);
                    responseVO.setMessage("失败");
                }
            }
        }

        return responseVO;
    }

    /**
     * 上传企业logo
     *
     * @param files
     * @return
     */
    @RequestMapping({"/logo"})
    public ResultVO uploadLogo(@RequestParam("file") MultipartFile[] files) {
        ResultVO responseVO = new ResultVO();
        MultipartFile[] var4 = files;
        int var5 = files.length;

        for (int var6 = 0; var6 < var5; ++var6) {
            MultipartFile file = var4[var6];
            if (file.isEmpty()) {
                responseVO.setCode(500);
                responseVO.setMessage("请上传正确的文件格式");
            } else {
                //得到文件名
                String filName = file.getOriginalFilename();
                //取出文件后缀
                String suffix = filName.substring(filName.lastIndexOf("."));
                //存入文件地址
                String perms = UUID.randomUUID() + suffix;
                String paths = path + perms;
                File newFile = new File(paths);
                // 如果pathAll路径不存在，则创建相关该路径涉及的文件夹;
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
                try {
                    //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
                    file.transferTo(newFile);
                    responseVO.setResult(true);
                    responseVO.setCode(200);
                    responseVO.setMessage("成功");
                    responseVO.setData("/files/" + perms);
                } catch (IOException var13) {
                    responseVO.setCode(-500);
                    responseVO.setMessage("失败");
                }
            }
        }
        return responseVO;
    }

    /**
     * 下载
     *
     * @param
     */
    @GetMapping("/download/{id}")
    @ResponseBody
    public ResultVO downloadInfo(@PathVariable String id) {
        ResultVO resultVO = new ResultVO();
        try {
            WordUtil wd = new WordUtil();
            Resume resume = resumeMapper.selectById(id);
            boolean b = wd.getData(resume);
            if (b) {
                resultVO.setCode(200);
                resultVO.setResult(true);
                resultVO.setData("D:/wordFile/");
            }
        } catch (Exception var5) {
            System.out.println(var5.toString());
            var5.printStackTrace();
            return ResultVO.ERROR;
        }
        return resultVO;
    }

    /*
    @RequestMapping({"/logo"})
    public ResultVO uploadLogo(@RequestParam("file") MultipartFile[] files) {
        ResultVO responseVO = new ResultVO();
        MultipartFile[] var4 = files;
        int var5 = files.length;

        for (int var6 = 0; var6 < var5; ++var6) {
            MultipartFile file = var4[var6];
            if (file.isEmpty()) {
                responseVO.setCode(500);
                responseVO.setMessage("请上传正确的文件格式");
            } else {
                String realPath = new String("src/main/resources/" + UPLOAD_PATH_PREFIX);
                File f = new File(realPath);

                File newFile = new File(paths);
                if (!f.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
                String filName = file.getOriginalFilename();
                String suffix = filName.substring(filName.lastIndexOf("."));
                String paths =UUID.randomUUID() + suffix;
                try {
                    String i =f.getAbsolutePath() + File.separator + paths;
                    File newFiles = new File(i);
                    //转存文件到指定路径，如果文件名重复的话，将会覆盖掉之前的文件,这里是把文件上传到 “绝对路径”
                    file.transferTo(newFiles);
                    responseVO.setResult(true);
                    responseVO.setCode(200);
                    responseVO.setMessage("成功");
                    responseVO.setData(UPLOAD_PATH_PREFIX+paths);
                } catch (IOException var13) {
                    responseVO.setCode(-500);
                    responseVO.setMessage("失败");
                }
            }
        }

        return responseVO;
    }
    */
}
